#include "../source/thread.h"
#include "../source/log.h"
#include "../source/util.h"
#include "../source/mutex.h"
#include "../source/config.h"
#include <yaml-cpp/yaml.h>
sylar::Logger::ptr g_logger = SYLAR_LOG_ROOT();
volatile long count = 0;
sylar::RWMutex s_mutex;

void fun1()
{
    usleep(rand()%10000);
    SYLAR_LOG_INFO(g_logger) 
    << "name :" <<sylar::Thread::GetName()
                             << " this.name" <<sylar::Thread::GetThis()->getName()
                             << " id: " << sylar::GetThreadId()
                             << " this.id: " << sylar::Thread::GetThis()->getId()
                            ;
    // for(int i=0;i<100000;++i){
    //     sylar::RWMutex::WriteLock lock(s_mutex);
    //     long x=count;
    //     ++x;
    //     count = x;
        
    // }
    // SYLAR_LOG_INFO(g_logger) << "name :" <<sylar::Thread::GetName() <<"end";
}

int n = 100000;
void fun2(){
    for(int i=0;i<n;++i){
        SYLAR_LOG_INFO(g_logger)<<"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    }
}

void fun3(){
    for(int i=0;i<n;++i){
        SYLAR_LOG_INFO(g_logger)<<"************************************";
    }
}

int main(int argc, char** argv)
{

    SYLAR_LOG_INFO(g_logger) << "thread test beigin";
    
    // YAML::Node root = YAML::LoadFile("/home/linker/workspace/sylar/bin/conf/log2.yml");
    // sylar::Config::LoadFromYaml(root);
    std::vector<sylar::Thread::ptr> thrs;

    for(int i=0;i<5;++i)
    {
        sylar::Thread::ptr thr(new sylar::Thread(&fun1, "name_"+std::to_string(i)));
        thrs.push_back(thr);
        
        // sylar::Thread::ptr thr1(new sylar::Thread(&fun2, "name_"+std::to_string(i)));
        // thrs.push_back(thr1);
        // sylar::Thread::ptr thr2(new sylar::Thread(&fun3, "name_"+std::to_string(i)));
        // thrs.push_back(thr2);
    }

    for(size_t i=0; i<thrs.size(); ++i)
    {
        thrs[i]->join();
    }
    
    SYLAR_LOG_INFO(g_logger) << "thread test end";
    SYLAR_LOG_INFO(g_logger) << "count =" << count;
    return 0;
}